/*
*  Copyright 2019-2020 Sun Feng
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.ruibo.modules.weixin.rest;

import com.ruibo.annotation.AnonymousAccess;
import com.ruibo.aop.log.Log;
import com.ruibo.modules.system.criteria.DeptQueryCriteria;
import com.ruibo.modules.system.service.dto.SysDeptDto;
import com.ruibo.modules.weixin.domain.RegionCode;
import com.ruibo.modules.weixin.repository.RegionCodeRepository;
import com.ruibo.modules.weixin.service.RegionCodeService;
import com.ruibo.modules.weixin.service.dto.RegionCodeDto;
import com.ruibo.modules.weixin.service.dto.RegionCodeQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author 孙峰
* @date 2020-11-06
**/
@RestController
@RequiredArgsConstructor
@RequestMapping("/weixin/regionCode")
public class RegionCodeController {

    private final RegionCodeService regionCodeService;

    private final RegionCodeRepository regionCodeRepository;
    @Log("导出数据")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, RegionCodeQueryCriteria criteria) throws IOException {
        regionCodeService.download(regionCodeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询RegionCode")
    public ResponseEntity<Object> query(RegionCodeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(regionCodeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增RegionCode")
    public ResponseEntity<Object> create(@Validated @RequestBody RegionCode resources){
        return new ResponseEntity<>(regionCodeService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改RegionCode")
    public ResponseEntity<Object> update(@Validated @RequestBody RegionCode resources){
        regionCodeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除RegionCode")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        regionCodeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @RequestMapping(value = "/getByParentCode", method = RequestMethod.POST, produces= MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> getByParentCode(final String parentCode){
        return new ResponseEntity(regionCodeRepository.findByParentCode(parentCode),HttpStatus.OK);
    }

    /**
     * 获取市县联动
     */
    @AnonymousAccess
    @GetMapping("/treeselect")
    public ResponseEntity<Object> treeselect(RegionCodeQueryCriteria regionCodeQueryCriteria) {
        List<RegionCodeDto> regionCodeDtoList = regionCodeService.queryAll(regionCodeQueryCriteria);
        return new ResponseEntity<Object>(regionCodeService.getRegionCodeTree(regionCodeDtoList), HttpStatus.OK);
    }

    /**
     * 获取市县联动
     */
    @AnonymousAccess
    @GetMapping("/treeSelectWithProvince")
    public ResponseEntity<Object> treeSelectWithProvince(RegionCodeQueryCriteria regionCodeQueryCriteria) {
        List<RegionCodeDto> regionCodeDtoList = regionCodeService.queryAll(regionCodeQueryCriteria);
        return new ResponseEntity<Object>(regionCodeService.getRegionCodeTreeWithProvince(regionCodeDtoList), HttpStatus.OK);
    }
}
